package com.hkyl.hospital.web.controller;

import com.github.pagehelper.PageInfo;
import com.hkyl.hospital.domain.ResultVo;
import com.hkyl.hospital.entity.Patient;
import com.hkyl.hospital.service.PatientService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author sjh
 * @date 2023/07/12 14:44:32
 */
@RestController
@RequestMapping("patient")
public class PatientController {

    @Autowired
    private PatientService patientService;

    Logger logger = LoggerFactory.getLogger(PatientController.class);

/*    @PostMapping("save")
    public ResultVo insert(@RequestBody Patient patient) {
        int rows = patientService.insert(patient);
        return rows > 0 ? ResultVo.success("添加成功") : ResultVo.failure("添加失败");
    }*/

    @PostMapping("delete")
    public ResultVo delete(@RequestBody Map map) {
        Integer patientId = (Integer) map.get("patientId");
        Integer status = (Integer) map.get("status");
        int rows = patientService.delete(patientId,status);
        return rows > 0 ? ResultVo.success("删除成功") : ResultVo.failure("删除失败");
    }

    @PostMapping("update")
    public ResultVo update(@RequestBody Patient patient) {
        logger.info(String.valueOf(patient));
        int rows = patientService.update(patient);
        return rows > 0 ? ResultVo.success("修改成功") : ResultVo.failure("修改失败");
    }


    @PostMapping("select")
    public ResultVo select(@RequestBody Map map) {
        Integer pageNum = (Integer) map.get("pageNumber");
        Integer pageSize = (Integer) map.get("pageSize");
        PageInfo<Patient> patients = new PageInfo<>(patientService.select(pageNum, pageSize));
        return ResultVo.success(patients);
    }

    @PostMapping("search")
    public ResultVo search(@RequestBody Map map){
        logger.info(map.toString());
        Integer pageNum = (Integer) map.get("pageNumber");
        Integer pageSize = (Integer) map.get("pageSize");
        String patientName = (String) map.get("patientName");
        String patientCard = (String) map.get("patientCard");
        logger.info(String.valueOf(pageSize));
        PageInfo<Patient> patients = new PageInfo<>(patientService.search(patientName, patientCard, pageNum, pageSize));
        return ResultVo.success(patients);
    }

    @PostMapping("login")
    public ResultVo login(@RequestBody Patient patient) {
        return ResultVo.success("登录成功", patientService.login(patient));
    }

    @PostMapping("register")
    public ResultVo register(@RequestBody Patient patient) {
        return ResultVo.success("注册成功", patientService.regisger(patient));
    }

    @PostMapping("resetpwd")
    private ResultVo resetPwd(@RequestBody String patientId) {
        logger.info(patientId);
        return patientService.resetpwd(Integer.valueOf(patientId)) > 0 ? ResultVo.success() : ResultVo.failure("");
    }
}
